package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysRoleDaoI;
import sy.model.SysRole;

@Repository("sysRoleDao")
public class SysRoleDaoImpl implements SysRoleDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysRole sysRole) {
		return getCurrentSession().save(sysRole);//直接用
		
	}

	@Override
	public void remove(SysRole sysRole) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysRole);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysRoleDaoI#update(sy.model.SysRole)
	 */
	@Override
	public void update(SysRole sysRole) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysRole);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysRoleDaoI#list()
	 */
	@Override
	public List<SysRole> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysRole").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysRoleDaoI#getSysRoleById(java.lang.String)
	 */
	@Override
	public SysRole getSysRoleById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysRole where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysRole> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysRoleDaoI#listSysRoleByKey(java.lang.String)
	 */
	@Override
	public List<SysRole> listSysRoleByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysRole where code like :key or name like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysRole> list = query.list();
		return list;
	}

}
